const router = require('koa-router')()

const userService = require('../control/control.js')


router.prefix('/users')

router.get('/recommend', async (ctx, next) => {
  const { sort } = ctx.request.query
  try {
    const data = await userService.findRecommend(sort)
    ctx.body = {
      code: '80000',
      data: data,
      msg: '查找成功'
    }
  } catch (error) {
    ctx.body = {
      code: '80002',
      data: error,
      msg: '服务器异常'
    }
  }
})
router.get('/picture', async (ctx, next) => {
  const { picId } = ctx.request.query
  try {
    const data = await userService.findpicture(picId)
    ctx.body = {
      code: '80000',
      data: data,
      msg: '查找成功'
    }
  } catch (error) {
    ctx.body = {
      code: '80002',
      data: error,
      msg: '服务器异常'
    }
  }
})
router.get('/detail', async (ctx, next) => {
  const { detailId } = ctx.request.query
  try {
    const data = await userService.findDetail(detailId)
    ctx.body = {
      code: '80000',
      data: data,
      msg: '查找成功'
    }
  } catch (error) {
    ctx.body = {
      code: '80002',
      data: error,
      msg: '服务器异常'
    }
  }
})
router.get('/login', async (ctx, next) => {
  const { phone, password } = ctx.request.query

  try {
    const data = await userService.userLogin(phone, password)
      ctx.body = {
        code: '80000',
        data: data,
        msg: '查找成功'
      }
  } catch (error) {
    ctx.body = {
      code: '80002',
      data: error,
      msg: '服务器异常'
    }
  }
})
router.get('/userdata', async (ctx, next) => {
  const { userId } = ctx.request.query
  try {
    const data = await userService.getUserData(userId)
    ctx.body = {
      code: '80000',
      data: data,
      msg: '查找成功'
    }
  } catch (error) {
    ctx.body = {
      code: '80002',
      data: error,
      msg: '服务器异常'
    }
  }
})
router.get('/findUserRecommend', async (ctx, next) => {
  const { userId } = ctx.request.query
  try {
    const data = await userService.findUserRecommend(userId)
    ctx.body = {
      code: '80000',
      data: data,
      msg: '查找成功'
    }
  } catch (error) {
    ctx.body = {
      code: '80002',
      data: error,
      msg: '服务器异常'
    }
  }
})
router.post('/writepic',async(ctx,next) => {
  const { picId,pic_1,pic_2,pic_3,pic_4,
    pic_5,pic_6,pic_7,pic_8,pic_9 } = ctx.request.body
  const result = await userService.writePic([ picId,pic_1,pic_2,pic_3,pic_4,
    pic_5,pic_6,pic_7,pic_8,pic_9])
    try {
   
      // console.log(result);
      if (result.affectedRows !== 0) {
        ctx.body = {
          code: '80000',
          data: 'ok',
          msg: '发布成功'
        }
      } else {
        ctx.body = {
          code: '80004',
          data: 'error',
          msg: '发布失败'
        }
      }
    } catch (error) {
      ctx.body = {
        code: '80002',
        data: error,
        msg: '服务器异常'
      }
    }
})
router.post('/writerec',async(ctx,next) => {
  const {userId,userName,userProfilePhoto
    ,userSignature,picId,detailId,head,serachKey } = ctx.request.body
  const result = await userService.writeRec([ userId,userName,userProfilePhoto
    ,userSignature,picId,detailId,head,serachKey])
    try {
   
      // console.log(result);
      if (result.affectedRows !== 0) {
        ctx.body = {
          code: '80000',
          data: 'ok',
          msg: '发布成功'
        }
      } else {
        ctx.body = {
          code: '80004',
          data: 'error',
          msg: '发布失败'
        }
      }
    } catch (error) {
      ctx.body = {
        code: '80002',
        data: error,
        msg: '服务器异常'
      }
    }
})
router.post('/writedet',async(ctx,next) => {
  const {detailId,title,content,userId } = ctx.request.body
  const result = await userService.writeDet([ detailId,title,content,userId])
    try {
   
      // console.log(result);
      if (result.affectedRows !== 0) {
        ctx.body = {
          code: '80000',
          data: 'ok',
          msg: '发布成功'
        }
      } else {
        ctx.body = {
          code: '80004',
          data: 'error',
          msg: '发布失败'
        }
      }
    } catch (error) {
      ctx.body = {
        code: '80002',
        data: error,
        msg: '服务器异常'
      }
    }
})
module.exports = router
